package com.iyao_app.im.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.iyao_app.R;
import com.iyao_app.base.BaseActivityWithHead;
import com.iyao_app.util.Contants;
import com.iyaowoqu.base_library.util.LogUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AboutUsActivity extends BaseActivityWithHead {


    @BindView(R.id.webView)
    WebView mWebView;
    @BindView(R.id.lin_root)
    LinearLayout mLinRoot;
    @BindView(R.id.head_view)
    RelativeLayout mHeadView;
    private String mUrl;
    private String extra;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_us);
        ButterKnife.bind(this);
        extra = getIntent().getStringExtra(Contants.OTHER_FIELD);
        mUrl = getIntent().getStringExtra(Contants.TYPE);
        if (!TextUtils.isEmpty(extra)){
            setTitleTV(extra);
        }
        webLoadUrl(mWebView, mUrl);
        LogUtil.e("===mul" + mUrl);
//        mWebView.setWebViewClient(new WebViewClient() {
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                if (url.startsWith("tel:")) {
//                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//                    startActivity(intent);
//                } else {
//                    view.loadUrl(url);
//                }
//                return true;
//            }
//
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                if (!mWebView.getSettings().getLoadsImagesAutomatically()) {
//                    mWebView.getSettings().setLoadsImagesAutomatically(true);
//                }
//            }
//        });
    }

    public void webLoadUrl(final WebView webView, final String url) {

        webView.post(new Runnable() {
            @Override
            public void run() {
                webView.loadUrl(url);
                WebSettings wSet = webView.getSettings();
                webView.requestFocusFromTouch();
                webView.setHorizontalScrollBarEnabled(false);//水平不显示
                webView.setVerticalScrollBarEnabled(false); //垂直不显示

//                wSet.setAllowFileAccess(true);
//                wSet.setUseWideViewPort(true);
//                wSet.setDomStorageEnabled(true);
//                wSet.setLoadWithOverviewMode(true);
//                wSet.setCacheMode(WebSettings.LOAD_NO_CACHE);
//                webView.setLayerType(View.LAYER_TYPE_HARDWARE,null);
//                webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//                webView.clearHistory();
//                webView.clearFormData();
//                webView.clearCache(true);
//                webView.setWebChromeClient(new MyWebChromeClient());

//                wSet.setJavaScriptEnabled(true);
//                wSet.setJavaScriptCanOpenWindowsAutomatically(true);
//                wSet.setCacheMode(WebSettings.LOAD_NO_CACHE);
//                wSet.setDomStorageEnabled(true);
//                wSet.setDatabaseEnabled(true);
//                wSet.setAppCacheEnabled(true);
//                wSet.setAllowFileAccess(true);
//                wSet.setSavePassword(true);
//                wSet.setSupportZoom(true);
//                wSet.setBuiltInZoomControls(true);
//                wSet.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
//                wSet.setUseWideViewPort(true);

                webView.setWebViewClient(new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView view, String url) { //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
                        view.loadUrl(url);
                        return true;
                    }

                    @Override
                    public void onPageFinished(WebView view, final String url) {
                        LogUtil.e("===" + url);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (extra != null)
                                    if (url.equals(mUrl)) {
                                        mHeadView.setVisibility(View.VISIBLE);
                                    } else {
                                        mHeadView.setVisibility(View.GONE);
                                    }
                            }
                        });
//                        view.getSettings().setJavaScriptEnabled(true);
                        super.onPageFinished(view, url);

                    }

                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {


//                        view.getSettings().setJavaScriptEnabled(true);
                        super.onPageStarted(view, url, favicon);
                    }

                    @Override
                    public void onReceivedError(WebView view, int errorCode,
                                                String description, String failingUrl) {

                        super.onReceivedError(view, errorCode, description, failingUrl);
                    }

                    @Override
                    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                        WebResourceResponse response = super.shouldInterceptRequest(view, url);

                        return response;
                    }
                });
            }
        });

    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    class MyWebChromeClient extends WebChromeClient {


        public MyWebChromeClient() {

        }


        @Override
        public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {

            return true;

            // return super.onJsConfirm(view, url, message, result);
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LogUtil.e("===mUrl==" + mUrl + "===" + mWebView.getUrl());
            if (mUrl.equals(mWebView.getUrl()) || !mWebView.getUrl().startsWith("http")) {
                finish();
            } else {
                mWebView.goBack();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.removeAllViews();
        mWebView = null;
        mLinRoot.removeAllViews();
    }
}
